//引入封装头信息和请求域名的axios对象
import http from './http'
//引入请求的URL地址

import { nowPlayingListUri } from '../config/url'
import { comingSoonListUri } from '../config/url'
import { moiveDetailUrl } from "@/config/url"
import { cityListUrl } from "@/config/url"
import { cinemaListUrl } from '@/config/url'
import { dlUrl } from '@/config/url'


//暴露出

//登录
export const userLogin = (data) => {
    return http.post(dlUrl, data)
}

export const nowplayingListData = (page = 1) => {
    http.defaults.headers.info = ''
    return http.get(nowPlayingListUri + page)
}
export const comingSoonListData = (page = 1) => {
    http.defaults.headers.info = ''

    return http.get(comingSoonListUri + page)
}

//暴露出 请求detail 详情页的数据方法
export const moiveDetail = (filmId) => {
    http.defaults.headers.info = 'info'
    return http.get(moiveDetailUrl + filmId)
}

//城市列表数据

export const cityListData = async() => {
    http.defaults.headers.info = "city";
    let ret = await http.get(cityListUrl);
    console.log(ret);
    let cities = ret.data.data.cities
    var codeList = [] //全部英文字母
    var dataList = [] //城市信息
    var indexList = [] //经过筛选的abcd 字母没有城市的字母剔除
    for (let i = 65; i <= 90; i++) {
        codeList.push(String.fromCharCode(i))
    }
    codeList.forEach((element) => {
        let temArr = cities.filter((item) => element.toLowerCase() == item.pinyin.substr(0, 1))
        if (temArr.length > 0) {
            // console.log(element);
            indexList.push(element)
            dataList.push({
                    index: element,
                    data: temArr
                })
                // console.log(dataList);
        }
    })
    return Promise.resolve([dataList, indexList])
}

//promise.resolve 是一个promise 封装 成功后带出的数据 嗯可以配合async await 使用


//即便是第二种方法 也会变成promise pending 被async感染成promise 
//return  [dataList,indexList]

//影院列表
export const cinemaListData = (id) => {
    http.defaults.headers.info = "cinema";
    return http.get(cinemaListUrl + id)
}